package com.eric.ztea.controller;

import java.sql.Time;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eric.ztea.dao.PriceRepository;
import com.eric.ztea.domain.Price;

@RestController
@RequestMapping("/api/price")
public class PriceController {

	@Autowired
    PriceRepository priceRepository;

	@GET
	@RequestMapping(value = "/findById")
	public Price findById(@RequestParam(value = "id", required = true) Long id) {
		return priceRepository.findById(id);
	}
	
	@GET
	@RequestMapping(value = "/findByName")
	public Price findByName(@RequestParam(value = "name", required = true) String name) {
		return priceRepository.findByName(name);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public Price save(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "startTime", required = true) Time startTime,
			@RequestParam(value = "endTime", required = true) Time endTime,
			@RequestParam(value = "price", required = true) String setprice,
			@RequestParam(value = "type", required = false) Long type,
			@RequestParam(value = "cityid", required = true) Long cityid) {
		return priceRepository.save(new Price(id, name, startTime, endTime, setprice, type, cityid));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		priceRepository.delete(id);
	}	
}
